
function createXMLHttpRequest() {
    var xmlHttp;
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}
function mostrarTareasDia(fecha){
     var xhr;
     var usuario = document.getElementById("usuario");
     var contrasena = document.getElementById("contrasena");
     xhr=createXMLHttpRequest();
     if(fecha==null){
            fecha=fechaActual();
     }
     var span=document.getElementById("fechaTarea");
     limpiar(span);
     span.appendChild(document.createTextNode(fecha));
     alert(fecha);
     xhr.open("GET","ManejadorTareas?action=obtenerTareas&fecha="+fecha,true);
     xhr.onreadystatechange=function(){

         mostrarTareas(xhr);
     };
     xhr.send(null);
}

function mostrarTareas(xhr){
//create the namespace object for this example
     var XML;
    if(xhr.readyState==4){
        if(xhr.status==200){
          XML=xhr.responseXML;
			YAHOO.namespace("kjjc.calendar");
			//define the lauchCal function which creates the calendar
			YAHOO.kjjc.calendar.launchCal = function() {

				//create the calendar object, specifying the container
				var myCal = new YAHOO.widget.Calendar("calendar");
				//draw the calendar on screen
				myCal.render();
				//get and display today's date
				var dayNum = myCal.today.getDay();
				var dayString = myCal.Locale.WEEKDAYS_LONG[dayNum];
				var date = myCal.today.getDate();
				var monthNum = myCal.today.getMonth();
				var monthString = myCal.Locale.MONTHS_LONG[monthNum];
				var year = myCal.today.getFullYear();

                                var tareas=XML.getElementsByTagName("tarea");

                                for(var i=0;i<tareas.length;i++){
                                    var tarea=tareas[i];
                                     var results1 = document.createElement("div");
                                     var nombre=document.createElement("p");
                                     nombre.appendChild(document.createTextNode(tarea.getElementsByTagName("nombre")[0].firstChild.nodeValue));
                                     var importancia=document.createElement("p");
                                     importancia.appendChild(document.createTextNode(tarea.getElementsByTagName("importancia")[0].firstChild.nodeValue));
                                     var tipo=document.createElement("p");
                                     tipo.appendChild(document.createTextNode(tarea.getElementsByTagName("tipo")[0].firstChild.nodeValue));
                                     var id=document.createElement("p");
                                     id.appendChild(document.createTextNode(tarea.getElementsByTagName("id")[0].firstChild.nodeValue));
                                     results1.appendChild(nombre);
                                     results1.appendChild(tipo);
                                     results1.appendChild(importancia);
                                     results1.appendChild(id);
                                     results1.setAttribute("class","tarea");
                                     results1.setAttribute("id","divTarea"+tarea.getElementsByTagName("id")[0].firstChild.nodeValue);
                                     //results1.setAttribute("onmousemove","dragDrop()");
                                     YAHOO.util.Dom.get("tareasDia").appendChild(results1);

                                }
                                dragDrop();

                                var ripDate = function(type, args) {
					//componenete de un d�a
					var dates = args[0];
					var date = dates[0];
					var theYear = date[0];
					var theMonth = date[1];
					var theDay = date[2];
                                        if (theMonth<10)
                                            theMonth="0"+theMonth;
                                        if (theDay<10)
                                            theDay="0"+theDay;
					var theDate = theDay + "/"+ theMonth + "/" + theYear;
                                        mostrarTareasDia(theDate);
					//es un document.getElementById
					//var field = YAHOO.util.Dom.get("fecha1");
					//field.value = theDate;

				}
				//se dispara en el momento de seleccionar un dia.
				myCal.selectEvent.subscribe(ripDate);

				/*


                                //work out date in 10 days time
				var futureDate = YAHOO.widget.DateMath.add(new Date("October 12, 1988"),YAHOO.widget.DateMath.DAY, 10);
				var results2 = document.createElement("div");
				results2.innerHTML = "In ten days time it will be " + futureDate;
				YAHOO.util.Dom.get("tareasDia").appendChild(results2);

				//work out date two months ago
				var pastDate = YAHOO.widget.DateMath.subtract(myCal.today,YAHOO.widget.DateMath.MONTH, 2);
				var results3 = document.createElement("div");
				results3.innerHTML = "Two months ago the date was " + pastDate;
				YAHOO.util.Dom.get("tareasDia").appendChild(results3);

				//work out day and week numbers of current date
				var numberOfDays = YAHOO.widget.DateMath.getDayOffset(myCal.today, year);
				var weekNumber = YAHOO.widget.DateMath.getWeekNumber(myCal.today, year);
				results4 = document.createElement("div");
				results4.innerHTML = numberOfDays + " days have elapsed so far this year";
				results5 = document.createElement("div");
				results5.innerHTML = "We are in week number " + weekNumber;

				YAHOO.util.Dom.get("tareasDia").appendChild(results4);
				YAHOO.util.Dom.get("tareasDia").appendChild(results5);*/
			}
			//create calendar on page load
			YAHOO.util.Event.onDOMReady(YAHOO.kjjc.calendar.launchCal);
			//attach listener for click event on calendar icon


}}}

function fechaActual(){
    var mydate=new Date();
	var year=mydate.getYear();
	if (year < 1000)
		year+=1900;
	var day=mydate.getDay();
	var month=mydate.getMonth()+1;
	if (month<10)
		month="0"+month;
	var day= mydate.getDate();
	if (day<10)
		day="0"+day;
            return day+"/"+month+"/"+year;
}
function limpiar(elemento){
    while(elemento.hasChildNodes()){
        alert("estoy limpiando");
        elemento.removeChild(elemento.firstChild);
    }
}